package edu.friday.controller;

import edu.friday.common.base.BaseController;
import edu.friday.common.constant.UserConstants;
import edu.friday.common.result.RestResult;
import edu.friday.common.security.LoginUser;
import edu.friday.common.security.service.TokenService;
import edu.friday.model.SysMenu;
import edu.friday.service.SysMenuService;
import edu.friday.utils.BeanUtils;
import edu.friday.utils.http.ServletUtils;
import edu.friday.utils.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import edu.friday.model.vo.SysMenuVO;
import java.util.List;

@RestController
@RequestMapping("system/menu")
public class SysMenuController extends BaseController {
    @Autowired
    private SysMenuService menuService;
    @Autowired
    private TokenService tokenService;

    @GetMapping("/list")
    public RestResult list(SysMenuVO menu){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<SysMenu> menus = menuService.selectMenuList(menu,userId);
        return RestResult.success(menus);
    }

    @PostMapping
    public RestResult add(@Validated @RequestBody SysMenuVO menuVO){
        SysMenu menu = new SysMenu();
        BeanUtils.copyPropertiesIgnoreNull(menuVO,menu);
        if(UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))){
            return RestResult.error("新增菜单"+menu.getMenuName()+"失败，名称已存在");
        }
        menu.setCreateBy("system");
        return toAjax(menuService.insertMenu(menu));
    }
    @PutMapping
    public RestResult edit(@RequestBody SysMenu menu){
        if(UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))){
            return RestResult.error("新增菜单"+menu.getMenuName()+"失败，名称已存在");
        }
        menu.setUpdateBy("system");
        return toAjax(menuService.updateMenu(menu));
    }
    @GetMapping(value = "/{menuId}")
    public  RestResult getinfo(@PathVariable Long menuId){
        return RestResult.success(menuService.selectMenuById(menuId));
    }
    @DeleteMapping("/{menuId}")
    public RestResult remove(@PathVariable("menuId")Long menuId){
        if(menuService.hasChildByMenuId(menuId)){
            return RestResult.error("存在子菜单，不允许删除");
        }
        if(menuService.checkMenuExistRole(menuId)){
            return RestResult.error("菜单已分配，不允许删除");
        }
        return toAjax(menuService.deleteMenuById(menuId));
    }
    @GetMapping("/treeselect")
    public RestResult treeselect(SysMenuVO menu){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<SysMenu> menus = menuService.selectMenuList(menu,userId);
        List<SysMenuVO> menuVOS = BeanUtils.copyProperties(menus,SysMenuVO.class);
        return  RestResult.success(menuService.buildMenuTreeSelect(menuVOS));
    }
    @GetMapping("/roleMenuTreeselect/{roleId}")
    public RestResult roleMenuTreeselect(@PathVariable("roleId") Long roleId){
        LoginUser loginUser =tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        SecurityUtils.getAuthentication();
        List<SysMenu> menus = menuService.selectMenuList(userId);
        RestResult ajax = RestResult.success();
        ajax.put("checkedKeys",menuService.selectMenuListByRoled(roleId));
        List<SysMenuVO> menuVOS = BeanUtils.copyProperties(menus,SysMenuVO.class);
        ajax.put("menus",menuService.buildMenuTreeSelect(menuVOS));
        return ajax;
    }
}
